package com.ply.schedule;

import com.ply.entity.vo.ProjectOption;
import com.ply.entity.vo.Result;
import com.ply.mapper.ProjectMapper;
import com.ply.service.impl.UserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author XiaoPan
 * date: 2022/1/19 10:36
 * <p>
 * action:
 */
@Component
@Slf4j
public class ProjectSchedule {
    @Autowired
    ProjectMapper projectMapper;

    @Autowired
    UserServiceImpl userService;

    @Scheduled(cron = "0 0/1 * * * ?")
    public void updateProject(){
        Result result = userService.queryAllProject();

        List<ProjectOption> projectOptions = (List<ProjectOption>) result.getDate();

        for (ProjectOption projectOption : projectOptions) {

            //如果 状态 不是 未开始 那么 跳过
            if (projectOption.getStatus() != 2){
                continue;
            }
            Integer id = projectOption.getId();

            //项目 开始 时间 判断
            Date now = new Date();
            Date startAt = projectOption.getStartAt();

            if (!(now.compareTo(startAt) < 0)){
                projectMapper.updateProjectStatus(id,3);
//                修改状态 修改为 开始
            }
            Date endAt = projectOption.getEndAt();

            if (!(now.compareTo(endAt) < 0)){
//                修改状态  为 已结束
                projectMapper.updateProjectStatus(id,4);
            }
        }

        log.info("执行定时器");
    }
}
